package com.juyu.miliao.api.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.service.MakeFriendsService;
import com.juyu.miliao.api.util.JacksonView;
import com.juyu.miliao.common.domain.Subject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 同城交友
 *
 * @author xuanqicheng
 * @create 2019-07-14:29
 */
@RestController
@RequestMapping("/makerFriends")
public class MakeFriendsController {

    @Autowired
    private MakeFriendsService makeFriendsService;

    /**
     *
     * @param token
     * @param location
     * @throws Exception
     */
    @GetMapping("/index")
    public void getLocation(Authentication token,String location) throws Exception {

        Subject subject = (Subject) token.getPrincipal();

        if(StringUtils.isBlank(location)){
            location = "";
        }
        makeFriendsService.getLocation(subject.getId(),location);
    }


    @PostMapping("/updateLocation")
    public void getUpdateLocation(Authentication token, String province,String city){
        Subject subject = (Subject) token.getPrincipal();
        makeFriendsService.getUpdateLocation(subject.getId(),province,city);
    }

    /**
     * 同城列表
     */
    @GetMapping("/list/{page}-{pageSize}")
    @JsonView(JacksonView.SameCityUser.class)
    public List<User> getList(Authentication token,@PathVariable("page") Integer page,
                          @PathVariable("pageSize") Integer pageSize){
        if(page>=2){
            return new ArrayList<>();
        }else{
            page = 1;
        }
        pageSize = 120;
        Subject subject = (Subject) token.getPrincipal();
       List<User> list= makeFriendsService.getSameCityList(subject.getId(),page,pageSize);
       return list;
    }


}
